<%@page import="domain.Alternative"%>
<%@page import="domain.Criteria"%>
<%@page import="center.DateUtil"%>
<%@page import="domain.Project"%>
<%@page import="java.util.List"%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
	String message = (String) request.getAttribute("message");
	Project project = (Project) request.getAttribute("project");
	List<Criteria> criterias = project.getCriterias();
	List<Alternative> alternatives = project.getAlternatives();
	boolean isFinish = true;
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%=project.getName() %></title>
<link href="./resources/style/mainStyle.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
	window.onload = function(){
		if("<%=message%>" != "null" ){
			alert("<%=message%>");
		}
	}
	
	function goToPreferencePage(criteriaId){
		document.getElementById("criteriaId").value = criteriaId;
		document.forms["perferenceForm"].submit();
	}
	
	function goToPreferenceGoalPage(){
		document.forms["perferenceGoalForm"].submit();
	}
</script>
</head>
<body>
	<br/>
	<div align="center">
		<div class='box' style="width: 60%" align="center">
			<div class='boxtop'><div></div></div>
		  		<div class='boxcontent'>
		  			<u><%= project.getName() %></u>
		  			<form action="./preference" method="get" id="perferenceForm">
		  				<input type="hidden" name="criteriaId" id="criteriaId"/>
		  				<input type="hidden" name="projectId" value="<%=project.getId() %>"/>
		  			</form>
		  			<form action="./preferenceGoal" method="get" id="perferenceGoalForm">
		  				<input type="hidden" name="projectId" value="<%=project.getId() %>"/>
		  			</form>
		  			<div align="left">
		  				<%if(project.getCriteriaPairs().size()==0){%>
		  					<a href="javascript:goToPreferenceGoalPage()"><%=project.getGoal() %></a>
		  					<% isFinish = false; %>
		  				<%}else{ %>
 							<li><%= project.getGoal()%></li>
 						<%} %>
			  			<ul>
			  				<%
			  					for(int i=0; criterias!=null && i<criterias.size(); i++){
			  						Criteria criteria = criterias.get(i);
			  						if(criteria.getSubOf()==null){
			  							List<Criteria> subCriterias = criteria.getSubCriterias();
			  				%>
			  						<li>
			  							<%if((criteria.isLeafNode() && criteria.getAlternativePairs().size()==0) || 
			  									(!criteria.isLeafNode() && criteria.getCriteriaPairs().size()==0)) {%>
			  								<a href="javascript:goToPreferencePage('<%=criteria.getId() %>')"><%= criteria.getName()%></a><%= criteria.getPriority()!=0? "("+criteria.getPriority()+")":""%>
			  								<% isFinish = false; %>
			  							<%}else{ %>
			  								<%if(criteria.isLeafNode()) {%>
			  								<form action="./result" method="get">
												<%= criteria.getName()%> <%= criteria.getPriority()!=0? "("+criteria.getPriority()+")":""%>
												<input type="hidden" name="criteriaId" value="<%=criteria.getId() %>">
			  									<input class="button white small" type="submit" value="view alternatives"/>
			  								</form>
			  								<%}else{ %>
			  									<%= criteria.getName()%> <%= criteria.getPriority()!=0? "("+criteria.getPriority()+")":""%>
			  								<%} %>
			  							<%} %>
			  							<ul>
			  							<%
			  								for(int j=0; subCriterias!=null && j<subCriterias.size(); j++){
			  									Criteria subCriteria = subCriterias.get(j);
			  							%>
			  								<%if(subCriteria.getAlternativePairs().size()==0){%>
				  								<li><a href="javascript:goToPreferencePage('<%=subCriteria.getId() %>')"><%=subCriteria.getName() %></a>
				  									<%= subCriteria.getPriority()!=0? "("+subCriteria.getPriority()+")":""%>
				  								</li>
				  								
				  								<% isFinish = false; %>
				  							<%}else{ %>
				  								<li>
				  								<form action="./result" method="get">
													<%= subCriteria.getName()%> <%= subCriteria.getPriority()!=0? "("+subCriteria.getPriority()+")":""%>
													<input type="hidden" name="criteriaId" value="<%=subCriteria.getId() %>">
				  									<input class="button white small" type="submit" value="view alternatives"/>
				  								</form>
				  								</li>
				  							<%} %>
			  								
			  							<%} %>
			  							</ul>
			  						</li>
			  					<%} %>
			  				<%} %>
			  			</ul>
			  			<br/>
			  			Alternatives:
			  			<ul>
			  				<%
			  					for(int i=0; alternatives!=null && i<alternatives.size(); i++){
			  						Alternative alternative = alternatives.get(i);
			  				%>
			  					<li><%= alternative.getName()%> <%=alternative.getPriority()!=0? "("+alternative.getPriority()+")":"" %></li>
			  				<%} %>
			  			</ul>
			  			<%if(isFinish){ %>
				  			<form action="./result" method="post">
				   				<input type="hidden" name="projectId" value="<%=project.getId() %>"/>
								<input class="button black" type="submit" value="Calculate Result" />
				   			</form>
				   		<%}else{ %>
				   			Please weight each criteria!! 
				   		<%} %>
		  			</div>
		  		</div>
		 	<div class='boxbottom'><div></div></div>
		</div>
	</div>
	
	<br/>
	<div align="center">
		<div class='box' style="width: 60%" align="center">
			<div class='boxtop'><div></div></div>
		  		<div class='boxcontent'>
		  			<form action="./criteria" method="post">
		  				<input type="hidden" name="projectId" value="<%=project.getId() %>"/>
		  				<select name="subOf" id="subOf">
							<option value="">--Select Supercriteria--</option>
							<%
								for( int i=0; criterias!=null && i<criterias.size(); i++){
									Criteria criteria = (Criteria)criterias.get(i);
									if(criteria.getSubOf()==null){
							%>
								<option value="<%=criteria.getId() %>"><%=criteria.getName() %></option>
								<%} %>
							<%} %>
						</select>
		  				<input type="text" size="30" name="criteriaName" id="criteriaName"/>
						<input class="button" type="submit" value="Create Criteria" />
		   			</form>
		  		</div>
		 	<div class='boxbottom'><div></div></div>
		</div>
	</div>
	
	<br/>
	<div align="center">
		<div class='box' style="width: 60%" align="center">
			<div class='boxtop'><div></div></div>
		  		<div class='boxcontent'>
		   			<form action="./alternative" method="post">
		   				<input type="hidden" name="projectId" value="<%=project.getId() %>"/>
		  				<input type="text" size="30" name="alternativeName" id="alternativeName"/>
						<input class="button" type="submit" value="Create Alternative" />
		   			</form>
		  		</div>
		 	<div class='boxbottom'><div></div></div>
		</div>
	</div>
	<br/>
</body>
</html>